//
//  GFCommentResturtantTableViewCell.m
//  GoodFood
//
//  Created by dllo on 15/11/2.
//  Copyright © 2015年 荣伟峰. All rights reserved.
//

#import "GFCommentResturtantTableViewCell.h"

@interface GFCommentResturtantTableViewCell ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView *commentTableView;
@property (nonatomic, strong) UIButton *commentButton;
@end

@implementation GFCommentResturtantTableViewCell
#pragma mark - 初始化
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
//        self.contentView.backgroundColor = [UIColor yellowColor];
        [self creatTableView];
        [self creatButton];
    }
    return self;
}
#pragma mark - 布局
- (void)layoutSubviews {
    [super layoutSubviews];
    CGFloat height = 0;
    NSArray *array = (NSArray *)self.model.comment;
    for (int i = 0; i < array.count; i ++) {
        NSDictionary *dic = [array objectAtIndex:i];
        GFCommentResturtantModel *commentModel = [GFCommentResturtantModel modWithDic:dic];
        height = height + [GFDetialCommentResturtantTableViewCell heightForCellWithComment:commentModel.comment];
    }

    self.commentTableView.frame = CGRectMake(0, 0, ScreenWidth, height);
    self.commentButton.frame = CGRectMake(0, height, ScreenWidth , HEIGHT - height);
}
#pragma mark - set
- (void)setModel:(GFResturtantDetialModel *)model {
    if (_model != model) {
        _model = model;
    }
    NSArray *array = (NSArray *)model.comment;
    if (array.count >= 5) {
        [self.contentView addSubview:self.commentButton];
    } else {
        [self.commentButton removeFromSuperview];
    }
}
#pragma mark - 创建tableView
- (void)creatTableView {
    self.commentTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 0, 0) style:UITableViewStyleGrouped];
    self.commentTableView.delegate = self;
    self.commentTableView.dataSource = self;
    self.commentTableView.bounces = NO;
    [self.contentView addSubview:self.commentTableView];
    [self.commentTableView registerClass:[GFDetialCommentResturtantTableViewCell class] forCellReuseIdentifier:@"comments"];
    self.commentTableView.layer.borderColor = [UIColor lightGrayColor].CGColor;
}
#pragma mark - row number
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (self.model.place_comment_count > 5) {
        return 5;
    } else {
        NSArray *array = (NSArray *)self.model.comment;
        return array.count;
    }
}
#pragma mark - cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    GFDetialCommentResturtantTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"comments"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    NSArray *arr = (NSArray *)self.model.comment;
    NSDictionary *dic = [arr objectAtIndex:indexPath.row];
    GFCommentResturtantModel *model = [GFCommentResturtantModel modWithDic:dic];
    cell.model = model;
    
    return cell;
}
#pragma mark - section head
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}
#pragma mark - setion foot
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}
#pragma mark - row height
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSArray *arr = (NSArray *)self.model.comment;
    NSDictionary *dic = [arr objectAtIndex:indexPath.row];
    GFCommentResturtantModel *model = [GFCommentResturtantModel modWithDic:dic];
    
    return [GFDetialCommentResturtantTableViewCell heightForCellWithComment:model.comment];
}
#pragma mark - 自定义高度
+ (CGFloat)heightForCellWithModel:(GFResturtantDetialModel *)model {
    CGFloat height = 0;
    NSArray *array = (NSArray *)model.comment;
    for (int i = 0; i < array.count; i ++) {
        NSDictionary *dic = [array objectAtIndex:i];
        GFCommentResturtantModel *commentModel = [GFCommentResturtantModel modWithDic:dic];
        height = height + [GFDetialCommentResturtantTableViewCell heightForCellWithComment:commentModel.comment];
    }

    return height;
}
#pragma mark - 创建button
- (void)creatButton {
    self.commentButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.commentButton setTitle:@"查看全部评论" forState:0];
    [self.commentButton setTintColor:[UIColor blackColor]];
    [self.commentButton addTarget:self action:@selector(button:) forControlEvents:UIControlEventTouchUpInside];
}
#pragma mark - button点击事件
- (void)button:(UIButton *)button {
    UIViewController *vc = [self viewController];
    GFCommentResturtantViewController *commentVc = [[GFCommentResturtantViewController alloc] initWithPlaceId:self.model.myId];
    [vc.navigationController pushViewController:commentVc animated:1];
}
#pragma mark - 找到vc
- (UIViewController*)viewController {
    for (UIView* next = [self superview]; next; next = next.superview) {
        UIResponder* nextResponder = [next nextResponder];
        if ([nextResponder isKindOfClass:[UIViewController class]]) {
            
            return (UIViewController*)nextResponder;
        }
    }
    return nil;
}
@end
